package com.ying.rabbitmqspringbootproducer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: TTLConfig
 * Package: com.ying.rabbitmqspringbootproducer.config
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/7/26 11:16
 * @Version 1.0
 */

@Configuration
public class TTLConfig {

    public static final String QUEUE_TTL_NAME = "ttlQueue";
    public static final String QUEUE_MESSAGE_TTL_NAME = "ttlMessageQueue";

    @Bean
    public Queue ttlQueue() {
        //设置x-message-ttl=毫秒值,该队列所有消息的最大存活时间
        return QueueBuilder.durable(QUEUE_TTL_NAME).withArgument("x-message-ttl",5000).build();
    }

    @Bean
    public Queue ttlMessageQueue() {
        return QueueBuilder.durable(QUEUE_MESSAGE_TTL_NAME).build();
    }

    @Bean
    public Binding bindingTTLQueue(Exchange confirmExchange, Queue ttlQueue) {
        return BindingBuilder.bind(ttlQueue).to(confirmExchange).with("error").noargs();
    }

    @Bean
    public Binding bindingTTLMessageQueue(Exchange confirmExchange, Queue ttlMessageQueue) {
        return BindingBuilder.bind(ttlMessageQueue).to(confirmExchange).with("delay").noargs();
    }
}
